package com.shunhe.storage.ui.goods;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.router_module.support.view.LinearItemDecoration;
import com.shunhe.storage.R;
import com.shunhe.storage.dao.model.Goods;
import com.shunhe.storage.ui.adapter.SwipeGoodsListAdapter;

import java.util.ArrayList;
import java.util.List;

// 货品列表(货品管理)
public class GoodsListActivity extends BaseActivity {
    private AppCompatSpinner sp_goods;
    private RecyclerView rcv_list;
    private TextView tv_add;// 添加货品
    private AppCompatEditText et_word;

    private SwipeGoodsListAdapter adapter;
    private List<Goods> goodsList = new ArrayList<>();

    private ArrayList<Goods> spinnerGoodsList = new ArrayList<>();
    private GoodsSpinnerAdapter spinnerAdapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_goods_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp_goods = findViewById(R.id.sp_goods);
        rcv_list = findViewById(R.id.rcv_list);
        tv_add = findViewById(R.id.tv_add);
        et_word = findViewById(R.id.et_word);

        setHeadTitle("货品管理");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new SwipeGoodsListAdapter(this);
        LinearItemDecoration divider = new LinearItemDecoration.Builder(this)
                .setSpan(R.dimen.deco_10_dp)
//                .setPadding(R.dimen.deco_10_dp)
//                .setLeftPadding(R.dimen.deco_10_dp)
//                .setRightPadding(R.dimen.deco_10_dp)
                .setColorResource(R.color.divider_list)
                .setShowLastLine(true)
                .build();
        rcv_list.addItemDecoration(divider);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        for (int i = 0; i < 5; i++) {
            Goods goods = new Goods();
            goods.setName("货品0");
            goodsList.add(goods);
        }
        adapter.setDataList(goodsList);

        // 设置下拉货品列表
        for (int i = 0; i < 5; i++) {
            Goods goods = new Goods();
            goods.setName("货品" + i);
            goods.setDesc("货品描述" + i);
            goods.setNum(i + "10");
            spinnerGoodsList.add(goods);
        }
        spinnerAdapter = new GoodsSpinnerAdapter(this, spinnerGoodsList);
        sp_goods.setAdapter(spinnerAdapter);

        setListeners();
    }

    private void setListeners() {
        setOnClickListener(tv_add);
        adapter.setOnSwipeListener(new SwipeGoodsListAdapter.OnSwipeListener() {
            @Override
            public void onDel(Object obj, int index) {
                Goods goods = (Goods) obj;
                ToastUtils.showShort("删除  index = " + index);
                goodsList.remove(index);
                adapter.setDataList(goodsList);
            }
        });

        // 下拉选择商品时 筛选 选中的货品 对应的货品列表
        sp_goods.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                Goods goods = spinnerGoodsList.get(position);
                String goodsName = goods.getName();
//                ToastUtils.showShort(goodsName);

                queryStockByGoods(goodsName);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        et_word.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String word = et_word.getText().toString().trim();
                if (TextUtils.isEmpty(word)) {
                    adapter.setDataList(goodsList);
                } else {
                    queryStockByGoods(word);
                }
                return false;
            }
        });
    }

    // 通过货品名称筛选货品库存信息
    private void queryStockByGoods(String goodsName) {
        List<Goods> tempGoodsList = new ArrayList<>();
        for (int i = 0; i < goodsList.size(); i++) {
            Goods goods = goodsList.get(i);
            if (goods.getName().trim().contains(goodsName.trim())) {// 符合筛选条件
                tempGoodsList.add(goods);
            }
        }
        adapter.setDataList(tempGoodsList);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_add) {// 添加货品
            startActivity(AddGoodsActivity.class);
        }
    }

    private class GoodsSpinnerAdapter extends BaseAdapter {
        private Context ctx;
        private ArrayList<Goods> dataList;

        public GoodsSpinnerAdapter(Context ctx, ArrayList<Goods> dataList) {
            this.ctx = ctx;
            this.dataList = dataList;
        }

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Goods getItem(int position) {
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(ctx, R.layout.item_goods_list_spinner, null);
                new GoodsSpinnerAdapter.ViewHolder(convertView);
            }
            GoodsSpinnerAdapter.ViewHolder holder = (GoodsSpinnerAdapter.ViewHolder) convertView.getTag();// get convertView's holder

            holder.car_brand.setText(getItem(position).getName());

            return convertView;
        }

        class ViewHolder {
            TextView car_brand;
//            TextView car_owner;
//            TextView car_cost;

            public ViewHolder(View convertView) {
                car_brand = convertView.findViewById(R.id.tv_name);
//                car_owner = (TextView) convertView.findViewById(R.id.tv_car_owner);
//                car_cost = (TextView) convertView.findViewById(R.id.tv_car_cost);
                convertView.setTag(this);//set a viewholder
            }
        }
    }
}